package com.sunkyc.framework.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunkyc.commons.entity.TablePageData;
import com.sunkyc.framework.utils.TablePageDataBuilderUtil;
import com.sunkyc.commons.entity.PageQueryVo;
import com.sunkyc.framework.system.entity.SysDictType;
import com.sunkyc.framework.system.mapper.SysDictTypeMapper;
import com.sunkyc.framework.system.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author sunkyc
 * @since 2022-05-31
 */
@Service
@RequiredArgsConstructor
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    private final SysDictTypeMapper baseMapper;


    @Override
    public Boolean dictTypeNameIsExistence(String dictTypeNmae) {
        return this.count(Wrappers.lambdaQuery(SysDictType.class).eq(SysDictType::getDictName,dictTypeNmae))>0;
    }

    @Override
    public Boolean dictTypeIsExistence(String dictType) {
        return this.count(Wrappers.lambdaQuery(SysDictType.class).eq(SysDictType::getDictType,dictType))>0;
    }

    @Override
    public int addDictType(SysDictType sysDictType) {
        return baseMapper.insert(sysDictType);
    }

    @Override
    public int updateDictType(SysDictType sysDictType) {
        return baseMapper.updateById(sysDictType);
    }

    @Override
    public TablePageData<SysDictType> queryList(PageQueryVo pageQueryVo, SysDictType sysDictType) {
        Wrapper<SysDictType> wrapper = buildWrapper(sysDictType);

        Page<SysDictType> page= new Page<>(pageQueryVo.getCurrent(),pageQueryVo.getPageSize());
        IPage<SysDictType> iPage = baseMapper.selectPage(page,wrapper);
        TablePageData<SysDictType> tablePageData = TablePageDataBuilderUtil.buildTablePageData(iPage);
        return tablePageData;
    }

    @Override
    public int batchDelete(Long[] dictIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(dictIds));
    }


    /**
     * 构建条件查询器
     * @param sysDictType
     * @return
     */
    private Wrapper<SysDictType> buildWrapper(SysDictType sysDictType){
        LambdaQueryWrapper<SysDictType> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNoneBlank(sysDictType.getDictName()),SysDictType::getDictName,sysDictType.getDictName())
                .eq(StringUtils.isNoneBlank(sysDictType.getDictType()),SysDictType::getDictType,sysDictType.getDictType())
                .eq(ObjectUtils.isNotEmpty(sysDictType.getState()),SysDictType::getState,sysDictType.getState());
        return wrapper;
    }

}
